package com.econage.eccm.test.base.webdatasource.datasourcebuilder;

import com.google.common.base.Preconditions;
import org.apache.tomcat.jdbc.pool.DataSourceFactory;

import javax.naming.Context;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Properties;

//重新设置一些参数
public class TomcatPoolBuilder implements DataSourceBuilder {
    private DataSourceFactory tomcatPoolFactory;
    private boolean autoCommitFalseForce;//true强制更改自动提交操作为false
    private boolean dataSourceNullForce = true;//强制删除dataSource配置
    private boolean dataSourceJndiNullForce = true;//强制删除dataSourceJndi配置

    public static TomcatPoolBuilder create(){
        return new TomcatPoolBuilder();
    }

    public TomcatPoolBuilder() {
        tomcatPoolFactory = new DataSourceFactory();
    }


    public TomcatPoolBuilder autoCommitFalseForce(boolean autoCommitFalseForce){
        this.autoCommitFalseForce = autoCommitFalseForce;
        return this;
    }
    public TomcatPoolBuilder dataSourceNullForce(boolean dataSourceNullForce){
        this.dataSourceNullForce = dataSourceNullForce;
        return this;
    }
    public TomcatPoolBuilder dataSourceJndiNullForce(boolean dataSourceJndiNullForce){
        this.dataSourceJndiNullForce = dataSourceJndiNullForce;
        return this;
    }


    @Override
    public DataSource build(Properties properties) throws SQLException {
        try {
            //参数检查
            Preconditions.checkNotNull(properties,"properties is null!");
            Preconditions.checkArgument(properties.getProperty(PROP_DRIVERCLASSNAME)!=null,"driverClassName is null!");
            Preconditions.checkArgument(properties.getProperty(PROP_PASSWORD)!=null,"password is null!");
            Preconditions.checkArgument(properties.getProperty(PROP_URL)!=null,"url is null!");
            Preconditions.checkArgument(properties.getProperty(PROP_USERNAME)!=null,"username is null!");

            Properties dsProperties = (Properties)properties.clone();

            //如果再配置文件里没有配置testWhileIdle参数，默认设为true，时间间隔默认
            if(dsProperties.getProperty(PROP_TESTWHILEIDLE)==null){
                dsProperties.setProperty(PROP_TESTWHILEIDLE,Boolean.TRUE.toString());
            }
            //如果未设置事务隔离等级，改为提交读
            if(dsProperties.getProperty(PROP_DEFAULTTRANSACTIONISOLATION)==null){
                dsProperties.setProperty(PROP_DEFAULTTRANSACTIONISOLATION,"READ_COMMITTED");
            }
            //强制改为不自动提交，配合归还连接时提交事务
            if(autoCommitFalseForce){
                dsProperties.setProperty(PROP_DEFAULTAUTOCOMMIT,Boolean.FALSE.toString());
            }
            //强制删除一些用不到的参数
            if(dataSourceNullForce){
                //剔除对外部dataSource的引用
                dsProperties.remove(PROP_DATASOURCE);
            }
            if(dataSourceJndiNullForce){
                //剔除对JndiDataSource的引用
                dsProperties.remove(PROP_DATASOURCE_JNDI);
            }

            return tomcatPoolFactory.createDataSource(dsProperties);
        } catch (Exception e) {
            throw new SQLException(e);
        }
    }
}
